package com.itheima.abvice;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect  //使用了面向切面编程的技术  表示这个类是一个切面类
@Component
public class RecordTimeAdvice {   //Aspece通知 增强
    //@Around("pt()")
    public Object abc(ProceedingJoinPoint pjp) {
        long begin = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Object object = null;  //业务方法
        try {
            object = pjp.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        log.info("执行了" + signature.getMethod().getName() + "方法..............耗时" + (end - begin) + "毫秒");
        return object;
    }

    @Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    public void pt() {

    }


    @Before("pt()")
    public void before() {   //执行原方法之前
        log.info("执行了before方法..............");

    }

    @AfterReturning("pt()")
    public void afterReturning() {   //正常执行原方法后
        log.info("执行了afterReturning方法..............");

    }

    @AfterThrowing("pt()")
    public void afterThrowing() {   //执行原方法出现了异常后
        log.info("执行了afterThrowing方法..............");

    }

    @After("pt()")
    public void after() {   //最终要执行的方法，无论是否出现异常都会执行
        log.info("执行了after方法..............");

    }


}
